package com.aabte.auth.group.infrastructure.persistence;

import com.aabte.auth.group.domain.model.Group;
import com.aabte.auth.group.domain.model.GroupId;
import com.aabte.auth.group.domain.repository.GroupRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.UUID;

@Repository
public class GroupRepositoryHibernate implements GroupRepository {

    @Resource
    private GroupDAO groupDAO;

    @Override
    public GroupId nextGroupId() {
        return new GroupId(UUID.randomUUID().toString().replaceAll("-", ""));
    }

    @Override
    public void addOrUpdate(Group group) {
        groupDAO.save(group);
    }

    @Override
    public Page<Group> find(Pageable pageable) {
        return groupDAO.findAll(pageable);
    }

    @Override
    public Optional<Group> find(GroupId groupId) {
        return groupDAO.findByGroupId(groupId);
    }

    @Override
    public void remove(Group group) {
        groupDAO.delete(group);
    }
}
